/**
 * Created by lixiaodi on 2019/06/28 0027.
 */
mini.parse();
var grid = mini.get("datagrid1");
var tree = mini.get("tree1");
var searchCols = mini.get("searchCols");//获得下拉框的ID
var searchKey = mini.get("searchKey")
var depIds;
//加载树结构
$(function () {

    //加载部门树
    treeHttp();
    //加载右侧显示数据（目前不用）
    //rightGridData()

});


//父页面给子页面传值
function SetData(data) {
    depIds = data.addData;
}


//子页面给父页面传值
function GetData() {

    //     //获取所有选中的nodes
    var checkedNodes = tree.getCheckedNodes()
    return checkedNodes;
}


//点击树节点的时候显示
tree.on("nodeselect", function (e) {
    //被选中得节点
    var selecNode = e.node;
    //如果点击了节点
    if (selecNode) {
        $.ajax({
            url: ajaxUrl + "/formltems/selectArchives.do?formListId=23",
//                                url:"http://192.168.31.65:8080/bpm/formltems/selectArchives.do",
            type: "post",
            dataType: "json",
            data: {
                condition: e.node.tabName,
                conValue: e.node.id
            },
            success: function (data) {
                grid_data = data.data;
                grid.setData(grid_data);
            },
            error: function (data) {
            }
        });
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});


//确定保存
function onSave() {
    var rows = grid.getSelecteds();
    if (rows) {
        CloseWindow("save");
    }
}


//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}


function onOk() {
    onSave();
}

function onCancel(e) {
    CloseWindow("cancel");
}


//tree请求
function treeHttp() {
    $.ajax({
        url: ajaxUrl + "/formltems/formTree.do",
        type: "post",
        data: {formListId: 23},
        success: function (data) {
            console.log(data);
            $.ajax({
                url: ajaxUrl + "/formList/findByFormListId.do?formListId=23",
                type: "post",
                dataType: "json",
                success: function (textFieldData) {
                    console.log(textFieldData);
                    textField = $.parseJSON(textFieldData.data).selecteds[0].cTableField;
                    console.log(textField, '树形textField');
                    tree.set({
                        textField: 'name'
                    });
                    tree.setData(data);
                    var clickNodeFlag = 0;
                    tree.on("nodeselect", function (e) {
                        console.log(e);
                        clickNodeFlag++;
                        treeSelected = e.selected;
                        nodes = tree.getAncestors(treeSelected);
                        nodes.push(treeSelected);
                        $.ajax({
                            url: ajaxUrl + "/formltems/selectArchives.do?formListId=23",
//                                url:"http://192.168.31.65:8080/bpm/formltems/selectArchives.do",
                            type: "post",
                            dataType: "json",
                            data: {
                                condition: e.node.tabName,
                                conValue: e.node.id
                            },
                            success: function (data) {
                                console.log(data);
                                grid_data = data.data;
                                grid.setData(grid_data);
                            },
                            error: function (data) {
                            }
                        });
                    })
                }
            })
        },
        error: function (data) {
            mini.alert("加载失败");
        }
    })
}


//加载树结构
function loadGrid() {

    //复杂档案
    if (menusFormTypes == 'complex') {
        $(".left").css({display: 'block'});
        $(".right").css("cssText", "width: calc(100% - 207px)!important;overflow:auto;");

        tree = mini.get("tree");
        //加载树形
        $.ajax({
            url: ajaxUrl + "/formltems/formTree.do?",
//        	url: "http://192.168.31.65:8080/bpm/formltems/formTree.do?",
            type: "post",
            dataType: "json",
            data: {
                formListId: 23
            },
            success: function (treeData) {
                console.log(treeData, 'treeData');
                $.ajax({
                    url: ajaxUrl + "/formList/findByFormListId.do?formListId=" + formId,
                    type: "post",
                    dataType: "json",
                    success: function (textFieldData) {
                        console.log(textFieldData.data);
                        console.log(typeof textFieldData.data);
                        condition = $.parseJSON(textFieldData.data).selecteds[0].cTableName;
                        console.log(textField, '树形textField')
                        tree.set({
                            textField: 'name'
                        });

                        tree.setData(treeData);
                        var clickNodeFlag = 0;
                        tree.on("nodeselect", function (e) {
                            console.log(e);
                            clickNodeFlag++;
                            treeSelected = e.selected;
                            console.log(treeSelected);
                            nodes = tree.getAncestors(treeSelected);
                            nodes.push(treeSelected);
                            $.ajax({
                                url: ajaxUrl + "/formltems/selectArchives.do",
//                                url:"http://192.168.31.65:8080/bpm/formltems/selectArchives.do",
                                type: "post",
                                dataType: "json",
                                data: {
                                    formListId: formId,
                                    condition: e.node.tabName,
                                    conValue: e.node.id
                                },
                                success: function (data) {
                                    console.log(data);
                                    grid_data = data.data;
                                    grid.setData(grid_data);
                                },
                                error: function (data) {
                                }
                            });
                        })
                    }, error: function (data) {
                    }
                });
            },
            error: function (data) {
            }
        });

    } else if (menusFormTypes == 'treeFile') {//单个树形

        $(".left").css({display: 'block'});
        $(".right").css("cssText", "width: calc(100% - 207px)!important;overflow:auto;");

        tree = mini.get("tree");
        //加载树形
        $.ajax({
            url: ajaxUrl + "/sqlAction/findsAll.do",
            type: "post",
            dataType: "json",
            data: {
                formListId: formId,
                menuId: menuId
            },
            success: function (treeData) {
                console.log(treeData, 'treeData');
                tree.set({
                    textField: cTableName + '_name'
                });
                tree.loadList(treeData, cTableName + "_id", "parent_level");
                tree.on("nodeselect", function (e) {
                    console.log(cTableName);
                    console.log(e.selected, 'dwadwadwa');
                    var cba = e.selected[cTableName + '_id'];
                    console.log(cba);

                    $.ajax({
                        url: ajaxUrl + "/sqlAction/findByParentLevel.do",
                        type: "post",
                        dataType: "json",
                        data: {
                            tableName: cTableName,
                            parentLevel: cba
                        },
                        success: function (data) {
                            console.log(data);
                            grid_data = data.data;
                            grid.setData(grid_data);
                        }, error: function (data) {
                        }
                    });
                });
            },
            error: function (data) {

            }
        });

    } else if (menusFormTypes == 'other') {//暂时不用
        $(".right").css({display: 'none'});
        $(".left").css("cssText", "width: 100% !important;overflow:auto;");
        tree = mini.get("tree");
        $.ajax({
            url: ajaxUrl + "/formltems/findFormSource.do",
//            url:"http://192.168.31.65:8080/bpm/formltems/findFormSource.do",
            type: "post",
            dataType: "json",
            data: {
                formListId: formId,
                menuId: menuId
            },
            success: function (data) {
                console.log(data, '数据');
                tree.set({
                    textField: 'name'
                });
                tree.loadList(data, "id", "parent_level");

                // 加载表格所有数据
                $.ajax({
                    url: ajaxUrl + "/formltems/findFormSource.do",
                    //url:"http://192.168.31.65:8080/bpm/formltems/findFormSource.do",
                    type: "post",
                    dataType: "json",
                    data: {
                        formListId: formId,
                        menuId: menuId
                    },
                    success: function (grid_data) {
                        console.log(grid_data, '数据');
                        grid.setData(grid_data);
                    },
                    error: function (data) {
                    }
                });
            },
            error: function (data) {
            }
        });
    } else {
        //$(".left").css({width: 0});
        $(".left").css({display: 'none'});
        $(".right").css("cssText", "width: 100% !important;overflow:auto;");
        //数据
        $.ajax({
            //url: ajaxUrl + "/sqlAction/findsAll.do",
            url: ajaxUrl + "/sqlAction/findsAll.do",
            //url:"http://192.168.31.65:8080/bpm/formltems/findFormSource.do",
            type: "post",
            dataType: "json",
            data: {
                formListId: formId,
                menuId: menuId
            },
            success: function (data) {
                console.log(data, '数据');
                grid_data = data;
                grid_data.forEach(function (item) {
                    switch (item[cTableName + '_state']) {
                        case '0':
                            item[cTableName + '_state'] = '未提交';
                            break;
                        case '1':
                            item[cTableName + '_state'] = '已提交';
                            break;
                        case '2':
                            item[cTableName + '_state'] = '审批中';
                            break;
                        case '3':
                            item[cTableName + '_state'] = '已终审';
                            break;
                        case '4':
                            item[cTableName + '_state'] = '已驳回';
                            break;
                        case '5':
                            item[cTableName + '_state'] = '已中止';
                            break;
                        case '6':
                            item[cTableName + '_state'] = '已撤回';
                            break;
                        default:
                            item[cTableName + '_state'] = '未提交';
                    }

                });
                grid.setData(grid_data);
            },
            error: function (data) {
            }
        });
    }
}

//加载右侧显示内容
function rightGridData() {

    $.ajax({
        // url: ajaxUrl + "/formltems/findByFormListIdToStyle.do",
        url: ajaxUrl + "/formltems/findsAll.do",
        type: "post",
        dataType: "json",
        data: {
            formListId: 23
        },
        success: function (data) {
            console.log(data, '列');
            for (var i = 0; i < data.length; i++) {
                if (data[i].tableType != 'table' && data[i].tableType != 'tab_table' && data[i].tableType != 'tab' && data[i].tableType != 'formname') {
                    cTableName = data[i].tableName;//获得主表名
                }
                if (i == 1) {
                    complex_add_field = data[i].cTreeStruct;
                }
            }
            cols = data;//列数据
            //cols.push(cols[cols.length-1])
            console.log(cols, '列赋值数据');
            var selectcol = mini.clone(data);
            console.log(selectcol);
            //选择下拉框渲染表头
            // var colss=data;
            var selectcols = selectcol.splice(1);
            console.log(selectcols);
            selectcols.unshift([]);
            // searchCols.setData(selectcols);
            grid.set({
                url: "",
                style: "width:100%;height:100%;",
                //id: "datagrid",
                columns: cols,
                multiSelect: true,
                allowResize: true,
                allowCellEdit: false,
                allowCellSelect: true,
                cellEditAction: "cellclick",
                allowRowSelect: true,
                showPager: false,
                editNextOnEnterKey: true,
                editNextRowCell: true
            });
            grid.render(document.getElementById("gridField"));

            // 加载表格所有数据
            $.ajax({
                // url: ajaxUrl + "/sqlAction/findsAll.do",
                url: ajaxUrl + "/sqlAction/findsAll.do",
                //url:"http://192.168.31.65:8080/bpm/formltems/findFormSource.do",
                type: "post",
                dataType: "json",
                data: {
                    formListId: 23,
                },
                success: function (grid_data) {
                    console.log(grid_data, '数据');
                    //处理数据（单据状态数字=》文字）
                    grid_data.forEach(function (item) {
                        switch (item[cTableName + '_state']) {
                            case '0':
                                item[cTableName + '_state'] = '未提交';
                                break;
                            case '1':
                                item[cTableName + '_state'] = '已提交';
                                break;
                            case '2':
                                item[cTableName + '_state'] = '审批中';
                                break;
                            case '3':
                                item[cTableName + '_state'] = '终审';
                                break;
                            case '4':
                                item[cTableName + '_state'] = '驳回';
                                break;
                            case '5':
                                item[cTableName + '_state'] = '中止';
                                break;
                            case '6':
                                item[cTableName + '_state'] = '撤回';
                                break;
                            default:
                                item[cTableName + '_state'] = '未提交';
                        }

                    });

                    grid.setData(grid_data);

                    loadGrid();
                    initBtn();

                    //	获取主子表关系
                    $.ajax({
                        url: ajaxUrl + "/tableClass/tableRelations.do",
                        // url: "http://192.168.31.65:8080/DSMP/tableClass/tableRelations.do",
                        type: "post",
                        // dataType: "json",
                        traditional: true,
                        data: {
                            formId: formId
                        },
                        success: function (dataMT) {
                            console.log(dataMT, '主子表关系');
                            mTableName = dataMT.mTableName;
                        },
                        error: function (data) {
                        }
                    });

                },
                error: function (data) {
                }
            });

        },
        error: function (data) {
        }
    });
}